/*
 * Copyright (C) 2011 Michael Widmer <widmm8@bfh.ch>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.bfh.aeneas.navigation.guide;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.Window;
import ch.bfh.aeneas.navigation.guide.GuideService.GuideServiceBinder;
import ch.bfh.aeneas.navigation.guide.GuideService.OnStateListener;

/**
 * GuideMe tries to get the bearing from a specified location to a specified 
 * target by respecting the current heading of the user.
 * @author Michael Widmer <widmm8@bfh.ch>
 */
public class GuideMe extends Activity {
    
    
    protected OnStateListener mListener = new OnStateListener() {

        public void onReady() {}
        public void onUpdate() {            
            compute();
        }        
    };
    
    protected ServiceConnection mGuideConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName arg0, IBinder arg1) {
            mCompassService = (GuideService) ((GuideServiceBinder) arg1).getService();
            mCompassService.setListener(mListener);
        }

        public void onServiceDisconnected(ComponentName arg0) {
        }
    };
    
    private final static String LOG_TAG = "GuideMe";
    
    protected GuideService mCompassService;
    protected Location mFrom;
    protected Location mTo;
    
    @Override
    /**
     * {@inheritDoc}
     */
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        Intent i = getIntent();
        if(i.hasExtra("from") && i.hasExtra("to")) {
            Bundle extras = i.getExtras();
            mFrom = (Location) extras.getParcelable("from");
            mTo = (Location) extras.getParcelable("to");
        } else {
            setResult(Activity.RESULT_CANCELED);
            finish();
        }
    }
    
    @Override
    /**
     * {@inheritDoc}
     */
    public void onResume() {
        super.onResume();
        
        bindService(new Intent(this, GuideService.class),mGuideConnection,Context.BIND_AUTO_CREATE);
    }
    
    @Override
    /**
     * {@inheritDoc}
     */
    public void onPause() {
        super.onPause();
        
        unbindService(mGuideConnection);
    }
    
    @Override
    /**
     * {@inheritDoc}
     */
    protected void onDestroy() {
        super.onDestroy();
        //unbindService(mGuideConnection);
    }

    /**
     * Computes the relative bearing (depending on the current heading of the user)
     * to the specified target.
     */
    protected void compute() {
        
        Log.i(LOG_TAG, "Compute values and output...");
        
        Log.i(LOG_TAG, "Setting target to coords: "+mFrom.getLatitude()+"/"+mFrom.getLongitude());
        Log.i(LOG_TAG, "Computing distance and bearing to "+mTo.getLatitude()+"/"+mTo.getLongitude());
        
        mCompassService.setTarget(mTo);
        int hour = mCompassService.getTimeAngle(mFrom);
        float distance = mFrom.distanceTo(mTo);
        
        Intent answer = new Intent();
        answer.putExtra("hour", hour);
        answer.putExtra("distance", distance);
        
        Log.i(LOG_TAG, "Bewegen Sie sich ca. "+distance+"m auf "+hour+" Uhr!");
        
        //unbindService(mGuideConnection);
        setResult(Activity.RESULT_OK, answer);
        finish();
    }
}
